Skip to content

fix(libSceNet): match FreeBSD's inet_pton and socket errors on macOS - #9231

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
Gonic0:fix/macos-sce-net-freebsd-errors
Oct 27, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
Gonic0:fix/macos-sce-net-freebsd-errors

Conversation

@Gonic0

@Gonic0 Gonic0 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What

guest_sce_net aborts on macOS at two checks where XNU's sockets library differs from FreeBSD's:

  • sceNetInetPton(AF_INET, "01.2.3.4") must return 0 (GuestSceNet.cpp:786). FreeBSD's inet_pton refuses an IPv4 component with a leading zero; macOS's accepts it and returns 1.2.3.4. On macOS sceNetInetPton now refuses such a component before calling the host.
  • sceNetSocket(AF_INET, SOCK_STREAM, IPPROTO_UDP) must fail with EPROTONOSUPPORT (GuestSceNet.cpp:151). FreeBSD's socket reports EPROTONOSUPPORT for a protocol of another socket type; XNU reports EPROTOTYPE. sceNetSocket only passes the stream, datagram and raw types, which XNU supports with protocol 0, so on macOS a host EPROTOTYPE there becomes EPROTONOSUPPORT.

Both changes are inside #ifdef __APPLE__; Linux and Windows are unchanged.

Commits:

  1. The leading-zero check in sceNetInetPton.
  2. The EPROTOTYPE mapping in sceNetSocket.

No debt entry: the guest sees FreeBSD's results.

Precedents: libkernel already follows FreeBSD here on every host. Its inet_pton parses with FreeBSD's text rules (#3530), and its socket reports EPROTOTYPE only for a type inet does not have (#7151). sceNetInetPton and sceNetSocket still call the host, so this PR only covers the two places where macOS's host differs.

Overlap: #8658 carries the same two hunks among its 100 commits. This PR takes them on their own so they can be reviewed and merged separately; whichever lands second drops or rebases them.

Tested

macOS 27.2, Apple M5 Pro, Apple clang 21.0.0, x86-64 build under Rosetta 2, Release.

main does not build libSceNet's dependencies on macOS, so the tests ran in a local tree of main 53a0a27 with the community macOS stack merged, with this branch's Export.cpp changes applied on top (the same diff as this branch on main cecb540).

  • Host behaviour, from a small C program on the same machine: inet_pton(AF_INET, "01.2.3.4") returns 1; socket(AF_INET, SOCK_STREAM, IPPROTO_UDP), socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP) and socket(AF_INET6, SOCK_STREAM, IPPROTO_UDP) fail with EPROTOTYPE (41); socket(AF_INET, SOCK_DGRAM, 99) fails with EPROTONOSUPPORT (43).
  • Without commit 1: guest_sce_net fails at GuestSceNet.cpp:786 and :787 for "01.2.3.4".
  • Without commit 2: guest_sce_net fails at GuestSceNet.cpp:151.
  • With both: no other check in guest_sce_net fails (checked with a non-aborting Require that prints the line), and ctest -R '^guest_sce_net$' passes 5 runs of 5.
  • Independently on an Apple M1, macOS 27.0.1, by @Mauro42K on feat: macOS host support (Apple Silicon via Rosetta 2, Vulkan on MoltenVK) #8658's e0a3ccec0 (comment): guest_sce_net passes 5/5 with both blocks and aborts with either one reverted.
  • tools/check_conventions.py --base upstream/main and git diff --check pass.

Not built on Linux or Windows; the changes are preprocessed out there.

Checklist

  • Based on current main; no other open PR implements the same functions (see Overlap above: feat: macOS host support (Apple Silicon via Rosetta 2, Vulkan on MoltenVK) #8658)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

Gonic0 and others added 2 commits October 21, 2026 12:52
FreeBSD's inet_pton refuses an IPv4 address with a component that has a
leading zero, such as "01.2.3.4", and guest_sce_net expects
sceNetInetPton to refuse it. macOS's inet_pton accepts it as 1.2.3.4, so
sceNetInetPton refuses it there before calling the host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…et type on macOS

FreeBSD's socket fails with EPROTONOSUPPORT when the protocol belongs to
another socket type, such as IPPROTO_UDP for SOCK_STREAM, and
guest_sce_net expects that error from sceNetSocket. XNU fails with
EPROTOTYPE instead. sceNetSocket only passes the stream, datagram and raw
types, which XNU supports with protocol 0, so a host EPROTOTYPE there is
always a protocol of another type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5836 core/libs/prx/libSceNet/Export.cpp
PR 9578 core/libs/prx/libSceNet/Export.cpp
PR 9932 core/libs/prx/libSceNet/Export.cpp
PR 10502 core/libs/prx/libSceNet/Export.cpp
PR 11134 core/libs/prx/libSceNet/Export.cpp

@EagleBearerYS

Copy link
Copy Markdown
Contributor

@Gonic0 a merge-order note: #9578 changes sceNetInetPton in the same Export.cpp. An AF_INET6 address now goes to libkernel's __inet_pton_nid_postfix, and an AF_INET address still goes to the host's inet_pton (InetPtonA on Windows). This PR's macOS check of an AF_INET address's leading zeros runs before that call, so with #9578 it still comes before the host parser. The two merge without a conflict in either order (git merge-tree of the two heads), so #9578 proposes that neither waits for the other. If you'd rather this one went first, say so and #9578 will list it under Depends on.

@Gonic0

Gonic0 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @EagleBearerYS, agreed. I ran the same git merge-tree check against #9494 and #9578 and got the same result: no conflicts, and both #ifdef __APPLE__ blocks stay in place. Neither needs to wait, so no Depends on needed on either side.

AI-assisted: yes

@maximilianfv

Copy link
Copy Markdown
Contributor

Independent test: Apple M1 Pro, macOS 27.0.1

Apple M1 Pro, macOS 27.0.1, Apple clang 21, x86-64 Release build under Rosetta 2, BUILD_TESTING=ON, no Vulkan loader. Base: mugurc's ci/macos-series-2 at 27e968974, with this pull request's own commits cherry-picked together with #9971, #9972, #8920, #9509, #9625, #9231 and #9733.

  • Without this pull request, guest_sce_net aborts at GuestSceNet.cpp:156 (sceNetSocket(..., SOCK_STREAM, IPPROTO_UDP)). With it, that check passes. Of the two pull requests applied, only this one adds the EPROTOTYPE mapping.
  • On this older series base, the test then stops at two checks that this pull request does not cover: :431 (a receive blocked on an unbound datagram socket must wake when the socket is shut down; macOS, like Winsock, does neither, and feat: macOS host support (Apple Silicon via Rosetta 2, Vulkan on MoltenVK) #8658 extends the Windows wait to macOS for it) and :1797 (sceNetResolverStartNtoa6 of "127.0.0.1", which main now runs through test hooks since 548aecf). Neither is a problem in this pull request.

Full ctest -j 6 (mic tests excluded): no test that passed before fails with this batch applied. precise_wait and guest_refused_connect are flaky here either way.

AI-assisted: yes (Claude Code ran the build and the tests).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
…macos-sce-net-freebsd-errors

Resolve the sceNetInetPton conflict with boykopovar#10275: the macOS leading-zero check moves into its convert lambda for AF_INET, so the dotted IPv4 tail of IPv6 text is checked too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@Gonic0

Gonic0 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@oneandonlydean when you have a moment, could you approve the workflow runs for this PR? Since it's my first contribution here, Build and Progress report are waiting on maintainer approval, so only conventions and labels have run so far. It's still mergeable with current main after #10275 and #10052 landed.

AI-assisted: yes

@maximilianfv

Copy link
Copy Markdown
Contributor

Retested at 7c5d97324 on Apple M1 Pro, macOS 27.0.1 (x86_64 build under Rosetta 2)

Applied to mugurc's macOS series (27e968974) merged with main at 1e20650e84, with other open macOS fixes, then toggled only this PR.

guest_sce_net
Without this PR Aborts at GuestSceNet.cpp:175 (CheckSocketHostErrors: sceNetSocket(..., SOCK_STREAM, IPPROTO_UDP) gets macOS's EPROTOTYPE (41) instead of EPROTONOSUPPORT (43))
With this PR Passes that check and runs on to GuestSceNet.cpp:845 (CheckIoctl), a separate gap

The other 19 tests in ctest -R '^(guest_sce_net|guest_inet_address|guest_net_.*|guest_datagram_.*|net_dontwait_.*|guest_socket_error)$' pass both ways.

The stop at :845 is not part of this PR: after one 5-byte datagram arrives, sceNetIoctl(FIONREAD) reports 37 instead of 5. libSceNet reads it with ioctl(FIONREAD), while libkernel's sockets already use SO_NREAD for datagram sockets on macOS (QueuedBytes in Socket/src/Udp.cpp).

AI-assisted: yes (Claude Code applied the PR, ran the tests, located both stops with lldb on a -O0 rebuild and wrote this report).

@harisibrar1 harisibrar1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked against main 522e32e and FreeBSD 11.4 lib/libc/inet/inet_pton.c and sys/kern/uipc_socket.c (socreate):

  • The leading-zero check rejects exactly when FreeBSD's inet_pton4 would reach if (saw_digit && *tp == 0) return (0); (inet_pton.c:94), so it never rejects anything FreeBSD accepts. Because the check is inside convert, it also covers the IPv6 dotted tail (::ffff:01.2.3.4, GuestSceNet.cpp:931), the same way FreeBSD's inet_pton6 hands that tail to inet_pton4.
  • In FreeBSD 11.4, socreate returns EPROTONOSUPPORT when a nonzero protocol doesn't match the type (uipc_socket.c:487-498), so mapping macOS's EPROTOTYPE to that error matches the reference for the SOCK_STREAM/IPPROTO_UDP case tested at GuestSceNet.cpp:176. I couldn't run this on macOS; that part rests on your and maximilianfv's host runs.
  • Main hasn't picked this up yet (no EPROTOTYPE handling in libSceNet/Export.cpp), and a 3-way git merge-tree against current main shows no conflicts. Looks good to me.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

@oneandonlydean
oneandonlydean merged commit 9014065 into boykopovar:main Oct 27, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants